﻿
Partial Class jDate
    Inherits System.Web.UI.UserControl

    Public Property DateTextFromValue() As String
        Get
            Return DateTextFrom.Text
        End Get
        Set(ByVal value As String)
            DateTextFrom.Text = value
        End Set
    End Property

    Public Property DateTextRequired() As Boolean
        Get
            Return DateTextFromRequired.Enabled
        End Get
        Set(ByVal value As Boolean)
            DateTextFromRequired.Enabled = value
        End Set
    End Property

    Public Property DateTextRequiredText() As String
        Get
            Return DateTextFromRequired.ErrorMessage
        End Get
        Set(ByVal value As String)
            DateTextFromRequired.ErrorMessage = value
        End Set
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Populate_MonthList()
            Populate_YearList()
            Dim dt As Date
            If DateTextFrom.Text <> "" Then
                dt = DateTextFrom.Text
                Calendar1.TodaysDate = dt
            End If

        End If
    End Sub

    ' SET THE FIRST CALENDAR (FROM DATE) 
    Protected Sub Set_Calendar(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim theDate As String = drpCalMonth.SelectedItem.Value + " 1, " + drpCalYear.SelectedItem.Value
        Dim dtFoo As DateTime = System.Convert.ToDateTime(theDate)
        Calendar1.TodaysDate = dtFoo
    End Sub

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Popup result is the selected date
        drpCalMonth.SelectedIndex = Calendar1.SelectedDate.Month - 1
        drpCalYear.SelectedItem.Selected = False
        drpCalYear.Items.FindByValue(Calendar1.SelectedDate.Year).Selected = True

        ' Popup result is the selected date
        ' PopupControlExtender1.Commit(Calendar1.SelectedDate.ToShortDateString())
    End Sub

    ' INNOVATIVE CALENDARS VIA AJAX THAT ALLOW USER TO PICK MONTH AND DATE VIA DROPDOWN
    Sub Populate_MonthList()
        drpCalMonth.Items.Add("January")
        drpCalMonth.Items.Add("February")
        drpCalMonth.Items.Add("March")
        drpCalMonth.Items.Add("April")
        drpCalMonth.Items.Add("May")
        drpCalMonth.Items.Add("June")
        drpCalMonth.Items.Add("July")
        drpCalMonth.Items.Add("August")
        drpCalMonth.Items.Add("September")
        drpCalMonth.Items.Add("October")
        drpCalMonth.Items.Add("November")
        drpCalMonth.Items.Add("December")

        Dim strMonth As String

        If DateTextFrom.Text = "" Then
            strMonth = DateTime.Now.ToString("MMMM")
        Else
            strMonth = CDate(DateTextFrom.Text.ToString).ToString("MMMM")
        End If

        drpCalMonth.Items.FindByValue(strMonth).Selected = True
    End Sub

    ' POPULATE THE YEARLIST FROM 20 YEARS AGO TO ONE YEAR HENCE 
    Sub Populate_YearList()
        Dim intYear As Integer

        ' Year list can be changed by changing the lower and upper 
        ' limits of the For statement
        For intYear = DateTime.Now.Year - 20 To DateTime.Now.Year + 1
            drpCalYear.Items.Add(intYear.ToString())
        Next
        Dim strYear As String

        If DateTextFrom.Text = "" Then
            drpCalYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = True
        Else
            strYear = CDate(DateTextFrom.Text.ToString).Year.ToString
            drpCalYear.Items.FindByValue(strYear).Selected = True
        End If

    End Sub

   
End Class
